Ext.onReady(function() {
	var Tree = Ext.tree;
	var tree = new Tree.TreePanel({
				id:'moduletree',
				width : 180,
				height : 500,
				useArrows : true,
				autoScroll : true,
				animate : true,
				singleExpand : false,
				containerScroll : true,  
				border : false,
				loader : new Ext.tree.TreeLoader({
							url : _ctxPath + '/genModuleTree.do'
						}),
				rootVisible : true,
				root : {
					nodeType : 'async',
					id : -1,
					text : '模块管理',
					expanded : true
				},

				tbar : new Ext.Toolbar({
							items : [{
										xtype : 'button',
										iconCls : 'silk-refresh',
										text : '刷新',
										handler : function() {
											tree.root.reload();
										}
									}, '-', {
										xtype : 'button',
										text : '展开',
										iconCls : 'silk-expand',
										handler : function() {
											tree.expandAll();
										}
									}, '-', {
										xtype : 'button',
										text : '收起',
										iconCls : 'silk-collapse',
										handler : function() {
											tree.collapseAll();
										}
									}]
						})
			});
tree.on("click",function (node,e){
if(node.isLeaf()){
e.stopEvent();
alert(node.id);
}
}); 
tree.on("contextmenu", function(node,e){
var menu = new Ext.menu.Menu( [
{
　 　 text :'添加',
　　　　 iconCls :'add',
　　　　handler : function(item, e) {
　　 editNode(node,"add"); }
}, {
　　　　 text :'编辑',
　　　　 iconCls :'edit',
　　　　 handler : function(item, e) {
　　 	  	editNode(node,"edit"); 
			  }
},{
　　　　 text :'删除',
　　　　 iconCls :'remove',
　　　　 handler : function(item, e) {remove(node);}
}]);
　　menu.showAt(e.getXY());
}); //end
function remove(node){
Ext.Msg.confirm('删除节点', '删除节点会同时删除其下所有子节点,你确认要删除<SPAN style="COLOR: red">'+node.text+'</SPAN>节点吗?',
function(btn, text) {
if (btn === 'yes') {
Ext.Ajax.request({url:_ctxPath+'delModule.do',params:{'module.id':node.id},
success:function(response,options){ Ext.Msg.alert('删除成功', '节点'+ node.text+ '删除成功');Ext.getCmp('moduletree').getRootNode().reload();},
failure : function(response, options) { Ext.Msg.alert('删除失败', '节点' + node.text+ '删除失败');}
});
}
});
}
function editNode(node,flaq){
//添加和编辑大窗口 ext-tree-edit-win
var win = new Ext.Window({ id :'ext-tree-edit-win',autoScroll :true, title :'节点维护',width :420, autoHeight:true });
var parentId = new Ext.form.TextField({id :'node-parent-id',name :'module.pid',hidden :true, hideLabel :true }); //parentId：父节点ID
var parentName = new Ext.form.TextField({ //parentName：父节点名称
　　 id :'node-parent-text',
　　 name :'parentname',
　　allowBlank:false, //是否允许为空
　　 msgTarget:'under',
blankText:'父节点名称不能为空',//提示信息
　　 width :80,
　　 fieldLabel :'父节点',
listeners : {
render : function(ct){
var parentEl = this.getEl().up('div.x-form-element');
if (parentEl) {
var child = parentEl.createChild({tag :'button', html :'选择父节点'});
child.on('click',function(btn){ if (Ext.getCmp('parent-node-select-win')){Ext.getCmp('parent-node-select-win').destroy();}
var win = new Ext.Window({
autoScroll :true,
id :'parent-node-select-win',
title :'选择父节点',
width :200,
height :350,
x :btn.getXY()[0] + 100,
y :btn.getXY()[1] - 50,
buttons:[{text :"确定",handler:function(btn,e){ win.destroy();}}]
});

if (!Ext.get('faceye-example-ext-tree-node-select-tree')) { Ext.getBody().createChild({tag :'div',id :'faceye-example-ext-tree-node-select-tree'}); }

var tree = new Ext.tree.TreePanel({
id :'faceye-example-ext-tree-node-select-tree-panel',
el :'faceye-example-ext-tree-node-select-tree',
autoScroll :true,
animate :true,
frame:true,
autoHeight :true,
border :false,
enableDD :true,
containerScroll :true,
rootVisible :true,
loader :new Ext.tree.TreeLoader({ dataUrl:'genModuleTree.do' }) }); //tree end
root = new Ext.tree.AsyncTreeNode({text:'根节点',draggable:false,id:-1});
tree.on('click', function(node,e) {parentId.setValue(node.id); parentName.setValue(node.text); if (node.isLeaf()) {e.stopEvent(); } else {node.toggle();}});
tree.setRootNode(root);
tree.render();
root.expand();
win.add(tree);
win.show();});} //if end
} //render 结束
}//listeners end
}); //parentName end

var editForm = new Ext.form.FormPanel( {
labelWidth :75,
url :_ctxPath + 'updateModule.do',
frame :true,
bodyStyle :'padding:5px 5px 0',
autoWidth :true,
autoHeight :true,
margins :'5 5 5 5',
monitorValid :true,
layout :'form',
defaultType :'textfield',
defaults : { width :200 },
reader :new Ext.data.JsonReader({success:true,
fields:[{name:'module.id',mapping:'id'},
{name:'module.name',mapping:'name'},
{name:'module.code',mapping:'code'},
{name:'module.url',mapping:'url'},
{name:'module.pid',mapping:'pid'},
{name:'module.paixu',mapping:'paixu'},
{name:'module.leaf',mapping:'leaf'},'parentname']}),//reader end
items : [parentId,
parentName,{
fieldLabel :'模块名称',
name :'module.name',
allowBlank:false,
msgTarget:'under',
blankText:'模块名称不能为空',
maxLength:50,
maxLengthText:'节点名称不能超过50个字符.'},
{
fieldLabel :'模块代码',
name :'module.code',
allowBlank:false,
msgTarget:'under',
blankText:'模块代码不能为空',
maxLength:50,
maxLengthText:'模块代码不能超过50个字符.'},
{ fieldLabel :'URL', name :'module.url'},
{xtype:'numberfield',fieldLabel :'排序',name :'module.paixu',msgTarget:'under'},
{
            xtype: 'radiogroup',
            name:'module.leaf',
            fieldLabel: '末级',
            items: [
                {boxLabel: '否', name: 'module.leaf', inputValue:false,checked:true},
                {boxLabel: '是', name: 'module.leaf', inputValue: true}
            ]
        },
{ name :'module.id',hidden :true, hideLabel :true }],
buttons : [ {
text :'提交',
formBind :true,
disabled :false,
type :'submit',
handler : function(btn, e) {
if (flaq=="edit"){editForm.getForm().submit({ method :'POST',waitMsg:'正在保存数据...',failure:function(){Ext.Msg.alert('提示消息','操作失败!!');},success:function(form, action) {Ext.getCmp('moduletree').getRootNode().reload(); win.destroy();}});}
if (flaq=="add"){editForm.getForm().submit({ method :'POST',url:_ctxPath+'addModule.do',waitMsg:'正在保存数据...',failure:function(){Ext.Msg.alert('提示消息','操作失败!!');},success:function(form, action) {Ext.getCmp('moduletree').getRootNode().reload(); win.destroy();}});}
}
}, {
text :'重置',
type :'submit',
handler : function(btn, e) { editForm.getForm().reset(); } }, {
text :'放弃',
type :'submit',
handler : function(btn, e) { win.destroy(); } } ] // buttons end
}); //editForm end
//editForm.getForm().clearInvalid(); 不起作用
if (flaq=="edit") {editForm.getForm().load({url :_ctxPath + 'viewModule.do',params : { 'module.id' :node.id }, waitMsg :'靖稍候...' });}
if (flaq=="add") {Ext.getCmp('node-parent-id').setValue(node.id);Ext.getCmp('node-parent-text').setValue(node.text);}
win.add(editForm);
win.show();
}//end 添加和修改
	tree.render('tree-div');
	tree.getRootNode().expand();
		// tree.expandAll();
});